#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

template <class Tp>
void chmax(Tp &a, const Tp &b) {
    a = max(a, b);
}

constexpr int MAXN = 200000 + 11;

int a[MAXN];

namespace subtask2 {
    constexpr int N = 100 + 11;
    
    int f[2][N][N];

    void solve(int n) {
        for (int w = 0; w < 2; ++w) {
            memset(f[w], 192, sizeof(f[w]));
        }

        int cur = 0;
        f[cur][0][0] = 0;
        for (int i = 1; i <= n; ++i) {
            for (int j = 0; j <= 100; ++j) {
                for (int k = 0; k <= 100; ++k) if (f[cur][j][k] >= 0) {
                    chmax(f[!cur][a[i]][k], f[cur][j][k] + (-(a[i] == j) & j));
                    chmax(f[!cur][j][a[i]], f[cur][j][k] + (-(a[i] == k) & k));
                }
            }

            memset(f[cur], 192, sizeof(f[cur]));
            cur ^= 1;
        }

        int ans = 0;
        for (int i = 0; i <= 100; ++i) {
            for (int j = 0; j <= 100; ++j) {
                chmax(ans, f[cur][i][j]);
            }
        }
        cout << ans << '\n';
    }
}

void solve() {
    //multitest please clear!
    int n;
    cin >> n;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
    }
    subtask2::solve(n);
}

//Hey I'm SkyWave!
int main() {
    freopen("color.in", "r", stdin);
    freopen("color.out", "w", stdout);
    // Must not forget these!

    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int T;
    cin >> T;
    while (T--) {
        solve();
    }
    return 0;
}